package com.example.shopping_li.goods;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.shopping_li.Fragment.BaseFragment;
import com.example.shopping_li.R;
import com.example.shopping_li.URLUtils;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class GoodsTypeFragment extends BaseFragment {
ListView lv;
    private List<GoodsTypeBean.DataBean> data;
    private GoodsAdapter adapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View view=inflater.inflate(R.layout.fragment_goods_type, container, false);
       lv=view.findViewById(R.id.frag_goods_lv_right);
        Bundle bundle1 = getArguments();
        int cid = bundle1.getInt("cid",1);
        String url= URLUtils.selCategoryById+cid;
        //数据源
        data=new ArrayList<>();
        //适配器
        adapter=new GoodsAdapter(getContext(),data);
        lv.setAdapter(adapter);
        //联网
        connectNet(url);
        setItemClick();
        return view;
    }
//每一条的点击事件
    private void setItemClick() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                GoodsTypeBean.DataBean bean = data.get(position);
//                Bundle bundle = new Bundle();
//                bundle.putSerializable("info",bean);
                //跳转详情页面
                Intent intent = new Intent(getActivity(),GoodsInfoActivity.class);
                intent.putExtra("info",bean);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onSuccess(String result) {
        //解析数据
        GoodsTypeBean bean = new Gson().fromJson(result, GoodsTypeBean.class);
        List<GoodsTypeBean.DataBean> list = bean.getData();
        data.addAll(list);//将得到的数据添加到数据源里
        adapter.notifyDataSetChanged();
    }
}